package  
{
	import adobe.utils.CustomActions;
	import as3isolib.display.primitive.IsoBox;
	import as3isolib.display.primitive.IsoPolygon;
	import com.smartfoxserver.v2.entities.variables.SFSUserVariable;
	import as3isolib.geom.IsoMath;
	import as3isolib.geom.Pt;
	//import as3isolib.graphics.IFill;
	import eDpLib.events.ProxyEvent;
	//import flash.display.Sprite;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author Bryan Keiren & Sander van Katwijk
	 */
	public class LocalPlayer extends Player
	{
		static private var m_Singleton:LocalPlayer = null;
		static public function Get():LocalPlayer	{	return m_Singleton;		}
		
		private var m_TargetPosBox:IsoBox = null;
		protected var m_IsoCircle:IsoPolygon = new IsoPolygon();
		
		private var m_PlayTimer:Number = 0;	// seconds.
		
		public function init():void
		{
			m_Singleton = this;
			
			m_TargetPosBox = new IsoBox();
			m_TargetPosBox.setSize(Globals.Cell_Size, Globals.Cell_Size, 0);
			m_TargetPosBox.moveTo(m_Pos.x, m_Pos.y, 0);
			m_TargetPosBox.fill = m_Team.getFill();
			
			m_TargetPosBox.container.mouseEnabled = false;
			
			
			
			Game.Get().getGrid().addEventListener(MouseEvent.CLICK, onMouseClick);
			//Engine.Get().addEventListener(MouseEvent.CLICK, onMouseClick);
		}
		public override function setPos(a_Pos:Vector2):void
		{
			for each(var flag:Flag in Flag.getFlags())
			{
				//Engine.printf("Dist = " + m_Pos.dist(flag.getPos()));
				if (m_Pos.distSq(flag.getPos()) < Globals.Flag_Capture_RadiusSq)	flag.attemptCapture(m_Team, false, true);
			}
			super.setPos(a_Pos);
			
			m_IsoCircle.moveTo(m_Pos.x, m_Pos.y, 0);
		}
		
		// Works in the same way as setPos does, except this function also sets the user variables
		// so that the change in position is synchronized across all clients.
		// Do NOT call this function each frame, since it will hog the server most likely.
		public function setPosSynched(a_Pos:Vector2):void
		{
			setPos(a_Pos);
			
			var newVars:Array = [];
			newVars.push(new SFSUserVariable("pos_x", m_Pos.x));
			newVars.push(new SFSUserVariable("pos_y", m_Pos.y));
			MessageHandler.Get().sendSetUserVariablesRequest(newVars);
		}
		
		public function LocalPlayer(a_Name:String, a_Pos:Vector2, a_Team:Team = null) 
		{
			super(a_Name, a_Pos, a_Team);
			
			m_IsoCircle = new IsoPolygon();
			m_IsoCircle.container.alpha = 0.5;
			var numPoints:int = 20;
			for (var i:int = 0; i < numPoints; ++i)
			{
				var cosresult:Number = Math.cos( (i * (360 / numPoints)) * (Math.PI / 180) );
				var sinresult:Number = Math.sin( (i * (360 / numPoints)) * (Math.PI / 180) );
				var x:Number = cosresult * m_VisionRadius;
				var y:Number = sinresult * m_VisionRadius;
				m_IsoCircle.pts.push(IsoMath.isoToScreen(new Pt( x, y, 0) ));
			}
		}
		
		public override function logic():void
		{
			super.logic();
		}
		
		public override function setTargetPos(a_Pos:Vector2):void
		{
			m_TargetPosBox.moveTo(a_Pos.x - m_TargetPosBox.width / 2, a_Pos.y - m_TargetPosBox.length / 2, 0);
			super.setTargetPos(a_Pos);
			
			var newVars:Array = [];
			newVars.push(new SFSUserVariable("target_x", m_TargetPos.x));
			newVars.push(new SFSUserVariable("target_y", m_TargetPos.y));
			MessageHandler.Get().sendSetUserVariablesRequest(newVars);
		}
		
		public function onMouseClick(a_Event:ProxyEvent):void
		{
			var mouseEvent:MouseEvent = MouseEvent(a_Event.targetEvent);
			//var p:Pt = new Pt(mouseEvent.localX, mouseEvent.localY);
			
			var p:Pt = new Pt(mouseEvent.localX, mouseEvent.localY);
			IsoMath.screenToIso(p);
			//Engine.printf("Click at [" + p.x + ", " + p.y + "]");
			
			var p_rounded:Pt = new Pt((Math.floor(p.x / Globals.Cell_Size) * Globals.Cell_Size) + (Globals.Cell_Size / 2),
							  (Math.floor(p.y / Globals.Cell_Size) * Globals.Cell_Size) + (Globals.Cell_Size / 2),
							  0);
			
			setTargetPos(new Vector2(p_rounded.x, p_rounded.y));
		}
		public override function setTeamColor():void
		{
			super.setTeamColor();
			m_TargetPosBox.fill = m_Team.getFill();
			m_IsoCircle.stroke = m_Team.getStroke();
		}
		public override function disable():void
		{
			Game.Get().getScene().removeChild(m_TargetPosBox);
			Game.Get().getScene().removeChild(m_IsoCircle);
			super.disable();
		}
		public override function enable():void
		{
			Game.Get().getScene().addChild(m_TargetPosBox);
			Game.Get().getScene().addChild(m_IsoCircle);
			super.enable();
		}
		public function incrementPlayTimer(a_Amount:Number):void
		{
			m_PlayTimer += Math.abs(a_Amount);
		}
		public function resetPlayTimer():void
		{
			m_PlayTimer = 0;
		}
		public function getPlayedTime():Number
		{
			return m_PlayTimer;
		}
	}
}